home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / EffectEdit / Glow.fx next >
Encoding:
Text File  |  2002-11-12  |  4.4 KB  |  167 lines

  1. //
  2. // Glow Effect
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Note: This effect file works with EffectEdit.
  6. //
  7.  
  8. string XFile = "tiger.x";   // model
  9. string BIMG  = "lake.bmp";  // Background image
  10. int    BCLR  = 0xff202080;  // background
  11.  
  12. // texture
  13. texture Tex0 < string name = "tiger.bmp"; >;
  14.  
  15. // transforms
  16. float4x3 WorldView  : WORLDVIEW; 
  17. float4x4 Projection : PROJECTION;
  18.  
  19. // light direction (view space)
  20. float3 LightDir < string UIDirectional = "Light Direction"; > = normalize(float3(0.0f, 0.0f, 1.0f));
  21.  
  22. // glow parameters
  23. float4 GlowColor     = float4(0.5f, 0.2f, 0.2f, 1.0f);
  24. float4 GlowAmbient   = float4(0.2f, 0.2f, 0.0f, 0.0f);
  25. float  GlowThickness = 0.015f;
  26.  
  27. struct VSTEXTURE_OUTPUT
  28. {
  29.     float4 Position : POSITION;
  30.     float4 Diffuse  : COLOR;
  31.     float2 TexCoord : TEXCOORD0;
  32. };
  33.  
  34. // draws unskinned object with one texture and one directional light.
  35. VSTEXTURE_OUTPUT VSTexture
  36.     (
  37.     float4 Position : POSITION, 
  38.     float3 Normal   : NORMAL,
  39.     float2 TexCoord : TEXCOORD0
  40.     )
  41. {
  42.     VSTEXTURE_OUTPUT Out = (VSTEXTURE_OUTPUT)0;
  43.   
  44.     float3 L = -LightDir;                                   // light direction (view space)
  45.     float3 P = mul(Position, WorldView);                    // position (view space)
  46.     float3 N = normalize(mul(Normal, (float3x3)WorldView)); // normal (view space)
  47.    
  48.     Out.Position = mul(float4(P, 1), Projection);   // projected position
  49.     Out.Diffuse  = max(0, dot(N, L));               // diffuse 
  50.     Out.TexCoord = TexCoord;                        // texture coordinates
  51.     
  52.     return Out;    
  53. }
  54.  
  55. struct VSGLOW_OUTPUT
  56. {
  57.     float4 Position : POSITION;
  58.     float4 Diffuse  : COLOR;
  59. };
  60.  
  61. // draws a transparent hull of the unskinned object.
  62. VSGLOW_OUTPUT VSGlow
  63.     (
  64.     float4 Position : POSITION, 
  65.     float3 Normal   : NORMAL
  66.     )
  67. {
  68.     VSGLOW_OUTPUT Out = (VSGLOW_OUTPUT)0;
  69.  
  70.     float3 N = normalize(mul(Normal, (float3x3)WorldView));     // normal (view space)
  71.     float3 P = mul(Position, WorldView) + GlowThickness * N;    // displaced position (view space)
  72.     float3 A = float3(0, 0, 1);                                 // glow axis
  73.  
  74.     float Power;
  75.  
  76.     Power  = dot(N, A);
  77.     Power *= Power;
  78.     Power -= 1;
  79.     Power *= Power;     // Power = (1 - (N.A)^2)^2 [ = ((N.A)^2 - 1)^2 ]
  80.     
  81.     Out.Position = mul(float4(P, 1), Projection);   // projected position
  82.     Out.Diffuse  = GlowColor * Power + GlowAmbient; // modulated glow color + glow ambient
  83.     
  84.     return Out;    
  85. }
  86.  
  87.  
  88.  
  89. technique TGlowAndTexture
  90. {
  91.     pass PTexture
  92.     {   
  93.         // single texture/one directional light shader
  94.         VertexShader = compile vs_1_1 VSTexture();
  95.         PixelShader  = NULL;
  96.         
  97.         // texture
  98.         Texture[0] = (Tex0);
  99.  
  100.         // sampler states
  101.         MinFilter[0] = LINEAR;
  102.         MagFilter[0] = LINEAR;
  103.         MipFilter[0] = POINT;
  104.  
  105.         // set up texture stage states for single texture modulated by diffuse 
  106.         ColorOp[0]   = MODULATE;
  107.         ColorArg1[0] = TEXTURE;
  108.         ColorArg2[0] = CURRENT;
  109.         AlphaOp[0]   = DISABLE;
  110.  
  111.         ColorOp[1]   = DISABLE;
  112.         AlphaOp[1]   = DISABLE;
  113.         
  114.     }
  115.  
  116.     pass PGlow
  117.     {   
  118.         // glow shader
  119.         VertexShader = compile vs_1_1 VSGlow();
  120.         PixelShader  = NULL;
  121.         
  122.         // no texture
  123.         Texture[0] = NULL;
  124.  
  125.         // enable alpha blending
  126.         AlphaBlendEnable = TRUE;
  127.         SrcBlend         = ONE;
  128.         DestBlend        = ONE;
  129.  
  130.         // set up texture stage states to use the diffuse color
  131.         ColorOp[0]   = SELECTARG2;
  132.         ColorArg2[0] = DIFFUSE;
  133.         AlphaOp[0]   = SELECTARG2;
  134.         AlphaArg2[0] = DIFFUSE;
  135.  
  136.         ColorOp[1]   = DISABLE;
  137.         AlphaOp[1]   = DISABLE;
  138.     }
  139. }
  140.  
  141. technique TGlowOnly
  142. {
  143.     pass PGlow
  144.     {   
  145.         // glow shader
  146.         VertexShader = compile vs_1_1 VSGlow();
  147.         PixelShader  = NULL;
  148.         
  149.         // no texture
  150.         Texture[0] = NULL;
  151.  
  152.         // enable alpha blending
  153.         AlphaBlendEnable = TRUE;
  154.         SrcBlend         = ONE;
  155.         DestBlend        = ONE;
  156.  
  157.         // set up texture stage states to use the diffuse color
  158.         ColorOp[0]   = SELECTARG2;
  159.         ColorArg2[0] = DIFFUSE;
  160.         AlphaOp[0]   = SELECTARG2;
  161.         AlphaArg2[0] = DIFFUSE;
  162.  
  163.         ColorOp[1]   = DISABLE;
  164.         AlphaOp[1]   = DISABLE;
  165.    }
  166. }
  167.